<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="受理编号" prop="bizNo">
        <el-input
          v-model="queryParams.bizNo"
          placeholder="请输入受理编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="vendorName">
        <el-input
          v-model="queryParams.vendorName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-if="user.userId !== 1"
          >录入汇总信息</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >批量导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          >批量导入</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleUpdataData"
          v-if="user.deptId === 205"
          >同步数据</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        :label="item.label"
        align="center"
        :prop="item.prop"
        v-for="(item, index) in column"
        :key="index"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="260px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleAgent(scope.row)"
            v-if="scope.row.status == 1 || scope.row.status == 3"
            >分配经办人</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 分配经办人 -->
    <el-dialog
      title="分配经办人"
      :visible.sync="open"
      width="500px"
      append-to-body
      @close="cancel"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="经办人" prop="agentId">
          <el-select clearable v-model="form.agentId" style="width: 100%">
            <el-option
              v-for="item in agentList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId + '/' + item.userName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 文件批量导入 -->
    <FileImport
      v-if="isFileImport"
      @onBack="
        (flag) => {
          this.isFileImport = false;
          if (flag) getList();
        }
      "
    />
    <!-- 添加/编辑/查看汇总信息 -->
    <Form
      :row="row"
      v-if="isForm"
      @onBack="
        (flag) => {
          this.isForm = false;
          if (flag) getList();
        }
      "
    />
  </div>
</template>

<script>
import Form from "./form.vue";
import FileImport from "./fileImport.vue";
import {
  vendorInfoList,
  vendorInfoBatchDelete,
  vendorInfoAllocation,
  updataSyncOldSystem,
} from "@/api/collect/collect/index";
import { getToken } from "@/utils/auth";
import { storage } from "@/utils/util.js";
export default {
  components: {
    Form,
    FileImport,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      //行内容
      row: {},
      // 用户信息
      user: storage.getStorage("user") || {},
      // 选中数组
      ids: [],
      column: [],
      columnObj: {
        206: [
          { label: "受理编号", prop: "bizNo" },
          { label: "申请人", prop: "applyUserName" },
          { label: "经办人", prop: "operatorName" },
          { label: "公司名称", prop: "vendorName" },
          { label: "申请事项", prop: "applyReason" },
          { label: "办结时限", prop: "finishDate" },
          { label: "经营范围", prop: "businessRange" },
          { label: "所属市州", prop: "cityName" },
          { label: "自编号", prop: "code" },
          { label: "注册地址", prop: "registerAddress" },
          { label: "仓库地址", prop: "warehouseAddress" },
          { label: "委托配送", prop: "entrustedDelivery" },
          { label: "创建时间", prop: "createTime" },
          { label: "状态", prop: "statusStr" },
          { label: "办理科室", prop: "deptName" },
          { label: "备注", prop: "remark1" },
        ],
        all: [
          { label: "受理编号", prop: "bizNo" },
          { label: "申请人", prop: "applyUserName" },
          { label: "经办人", prop: "operatorName" },
          { label: "公司名称", prop: "vendorName" },
          { label: "申请事项", prop: "applyReason" },
          { label: "创建时间", prop: "createTime" },
          { label: "状态", prop: "statusStr" },
          { label: "办理科室", prop: "deptName" },
          { label: "备注", prop: "remark1" },
        ],
      },
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 添加/编辑/查看汇总信息状态
      isForm: false,
      // 文件批量导入状态
      isFileImport: false,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bizNo: undefined,
        vendorName: undefined,
      },
      // 表单参数
      form: { agentId: "" },
      // 经办人列表
      agentList: [],
      // 表单校验
      rules: {
        agentId: [
          {
            required: true,
            message: "经办人不能为空",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  created() {
    if (this.columnObj[this.user.deptId]) {
      this.column = this.columnObj[this.user.deptId];
    } else {
      this.column = this.columnObj.all;
    }
    this.getList();
    this.getUserList();
  },
  methods: {
    getList() {
      this.loading = true;
      vendorInfoList(this.queryParams).then((response) => {
        this.postList = response.rows || [];
        this.total = response?.total || 0;
        this.loading = false;
      });
    },

    getUserList() {
      this.GetUserList((data) => {
        this.agentList = data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 提交表单 */
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let array = this.form.agentId.split("/");
          let operatorId = array[0];
          let operatorName = array[1];
          vendorInfoAllocation({
            operatorId,
            operatorName,
            id: this.row.id,
          }).then((response) => {
            this.Message("分派成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.row = selection[0];
    },

    /** 查看 */
    handleView(row) {
      if (this.toProductionApp()) {
        return;
      }
      row.isView = true;
      this.row = row;
      this.isForm = true;
    },

    /** 新增 */
    handleAdd() {
      if (this.toProductionApp()) {
        return;
      }
      this.row = {};
      this.isForm = true;
    },
    /** 编辑 */
    handleUpdate(row) {
      if (this.toProductionApp()) {
        return;
      }
      row.isView = false;
      this.row = row;
      this.isForm = true;
    },

    /** 删除 */
    handleDelete() {
      if (this.toProductionApp()) {
        return;
      }
      if (this.ids.length === 0) {
        this.$message.warning("请选择要删除的数据");
        return;
      }
      const ids = this.ids;
      this.$modal.confirm("是否确认删除数据项？").then(() => {
        vendorInfoBatchDelete({ ids: ids }).then((response) => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        });
      });
    },

    /** 分配经办人 */
    handleAgent(row) {
      if (this.toProductionApp()) {
        return;
      }
      this.row = row;
      this.open = true;
    },
    /** 批量导入 */
    handleImport() {
      if (this.toProductionApp()) {
        return;
      }
      this.isFileImport = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "vendor/info/export",
        {
          ...this.queryParams,
        },
        `信息汇总表_${new Date().getTime()}.xlsx`,
        {
          headers: { "Content-Type": "application/json;charset=utf-8" },
          isTansParams: true,
        }
      );
    },
    /** 同步数据，仅生产科存在 */
    handleUpdataData() {
      updataSyncOldSystem({}).then(() => {
        this.Message("同步成功");
        this.getList();
      });
    },
    /**  生产科跳转  */
    toProductionApp() {
      let flag = this.user.deptId === 205;
      if (flag) {
        window.open(
          `${process.env.VUE_APP_PRODUCTION_URL}?username=${this.user.phonenumber}`
        );
        this.$message.warning("请在生产科系统操作");
      }
      return flag;
    },
  },
};
</script>
